package com.design.softwaredesign.controller;

import com.design.softwaredesign.entity.Good;
import com.design.softwaredesign.front.GoodInfo;
import com.design.softwaredesign.service.GoodService;
import com.design.softwaredesign.utils.OssUtil;
import com.design.softwaredesign.vo.DataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :WaiterXiaoYY
 * @description: TODO
 * @data :2020/10/26 20:47
 */
@Api(description = "闲置物品控制层")
@RestController
@RequestMapping("/web")
public class GoodsController {
    @Autowired
    private OssUtil ossUtil;
    @Autowired
    private GoodService goodService;

    @GetMapping("/getGoodID")
    public DataVo getGoodID() {
        DataVo dataVo = new DataVo();
        List<String> id = new ArrayList<>();
        id.add(UUID.randomUUID().toString());
        dataVo.setMsg("商品id生成成功");
        dataVo.setCode(200);
        dataVo.setData(id);
        return dataVo;
    }

    @PostMapping("/uploadImage")
    public DataVo uploadImage(@RequestParam("goodId") String goodId, @RequestParam("file") MultipartFile file) {
        DataVo dataVo = new DataVo();
        String url = "";
        try {
            url = ossUtil.checkImage(file);
            goodService.addImage(goodId, url);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return dataVo;
        }
    }

    @PostMapping("/addGood")
    public DataVo addGood(@RequestBody GoodInfo goodInfo) {
        return goodService.addGood(goodInfo);
    }

    @GetMapping("/getGoodsCategory")
    @ApiOperation(value = "获取商品类型")
    public DataVo getGoodsCategory() {
        return goodService.getGoodsCategory();
    }

    @GetMapping("/getGoodsCategorylist")
    @ApiOperation(value = "获取商品类型商品列表")
    public DataVo getGoodsCategoryListData(Integer pageNum, Integer pageSize, String query, String userId) {
        return goodService.getGoodsCategoryListData(pageNum, pageSize, query, userId);
    }

    @GetMapping("/getgoodslist")
    @ApiOperation(value = "获取商品列表")
    public DataVo getGoodsList(Integer goodCategoryId, Integer pageNum, Integer pageSize, String query, String userId) {
        return goodService.getGoodsList(goodCategoryId, pageNum, pageSize, query, userId);
    }

    @PostMapping("/updateGoodInfo")
    @ApiOperation(value = "修改商品信息")
    public DataVo updateGoodInfo(@RequestBody GoodInfo goodInfo) {
        return  goodService.updateGoodInfo(goodInfo);
    }

    @PostMapping("/deleteGoodInfoById")
    @ApiOperation(value = "删除商品信息")
    public DataVo deleteGoodInfoById(@RequestParam("goodId") String goodId) {
        return  goodService.deleteGoodInfoById(goodId);
    }

    @PostMapping("/changeGoodStateById")
    @ApiOperation(value = "修改商品状态")
    public DataVo changeGoodStateById(@RequestParam("goodId") String goodId, @RequestParam("phone") String phone) {
        return  goodService.changeGoodStateById(goodId, phone);
    }

    @GetMapping("getgoodbygoodid")
    public DataVo getGoodByGoodId(String goodId, String userId) {
        return goodService.getGoodByGoodId(goodId, userId);
    }
}
